/**
 * layMin弹出框简单版 v 1.1.0
 * 
 * @author yyq
 * @date 2022-08-22
 */


/**
 * -------------------------------------------------------------------------------------------------------------------------------
 * 
 * 【照片 photo】 例如layMin.photo({src: 'img/1.jpg'})
 * 
 * 【提示 tips】 例如： lay = layMin.tips({elem: "body", msg: "操作成功", offset: 'right', time: 3})
 * elem: 指定标签内（默认body）
 * style：自定义样式
 * color：图标颜色
 * msg：提示内容
 * icon：显示图标（如果图标没有传参，默认根据提示来选择）
 * line：是否显示倒计时线条
 * time：秒
 * offset: top/right
 * stype：是否提示停止自动关闭类型：1 鼠标放上后停止，移开后继续 2 鼠标放上后停止，需手动关闭
 * timeDesc：是否开启倒计时数字
 * anim：背景开启闪烁
 * 
 * 【按钮提交加载loadSubmit】
 * 【按钮提交关闭closeSubmit】
 * 
 * 【加载中load】 例如：var index = layMin.load({elem: "body", bg: 'white'});
 * elem: 指定标签内（默认body）
 * bg: 背景填充颜色
 * fontSize: 图标大小
 * color: 图标颜色
 * time：毫秒
 * icon：1点点圈 2半圆
 * type: 加载类型
 * 
 */
var layMin = {
	indexList: [],
	index: 9999999999,
	photo: function(data){
		layMin.index = layMin.index + 1;
		var layIndex = layMin.index;
		layMin.indexList.push(layIndex);
		var s = '';
		s += '<div class="lay-min-photo-bg lay-min-index-'+layIndex+'" style="z-index:'+layIndex+';"></div>';
		s += '<div class="lay-min-photo-box lay-min-index-'+layIndex+'" style="z-index:'+(layIndex + 1)+'">';
		s += '	<img src="'+data.src+'">';
		s += '</div>';
		$(data.elem || "body").append(s);
		// 点击关闭
		$('.lay-min-index-'+layIndex).on("click", function(){
			layMin.close(layIndex);
			layMin.stopPropagation();
		})
		// 图片预览-鼠标滚动缩放
		$(document).on("mousewheel DOMMouseScroll", ".lay-min-photo-box img", function (e) {
		    var delta = (e.originalEvent.wheelDelta && (e.originalEvent.wheelDelta > 0 ? 1 : -1)) || // chrome & ie
		        (e.originalEvent.detail && (e.originalEvent.detail > 0 ? -1 : 1)); // firefox
		    var image = $(".lay-min-photo-box img");// 弹出的图片
		    var h = image.height();
		    var w = image.width();
		    if (delta > 0) {
		        if (h < (window.innerHeight)) {
		            h = h * 1.05;
		            w = w * 1.05;
		        }
		    } else if (delta < 0) {
		        if (h > 100) {
		            h = h * 0.95;
		            w = w * 0.95;
		        }
		    }
		    image.height(h);
		    image.width(w);
		});
		var img = new Image();
		img.src = data.src;
		img.onload = function(e){
			var imgWidth = this.width;
			var imgHeight = this.height;
			var w = $(window).width();
			var h = $(window).height();
			var obj = layMin.setWH(w, imgWidth, imgHeight, 1);// 计算宽度（不得超出浏览器宽度）
			imgWidth = obj.imgWidth;
			imgHeight = obj.imgHeight;
			var obj = layMin.setWH(h, imgWidth, imgHeight, 2);// 计算高度（不得超出浏览器高度）
			imgWidth = obj.imgWidth;
			imgHeight = obj.imgHeight;
			$('.lay-min-index-'+layIndex).find("img").css({'width': imgWidth+'px', 'height': imgHeight+'px'});
		};
		// 成功回调
		if(data.success){
			data.success();
		}
		return layIndex;
	},
	setWH: function(wh, imgW, imgH, type){
		if(type == 1){
			if(imgW >= wh){
				imgW = imgW / 2;
				imgH = imgH / 2;
				return layMin.setWH(wh, imgW, imgH, type);
			}else{
				return {'imgWidth': imgW, 'imgHeight': imgH};
			}
		}else{
			if(imgH >= wh){
				imgW = imgW / 2;
				imgH = imgH / 2;
				return layMin.setWH(wh, imgW, imgH, type);
			}else{
				return {'imgWidth': imgW, 'imgHeight': imgH};
			}
		}
	},
	tips: function(data){
		if(!data){
			data = {};
		}
		if(data.time){
			data.time = data.time > 60 ? 60 : data.time;
		}
		var offset = (data.offset||'right');
		var len = $(".lay-min-tips-"+offset).length;
		if(len < 1){
			$(data.elem || "body").append('<div class="lay-min-tips '+("lay-min-tips-"+offset)+'" style="z-index:'+(layMin.index+1000)+';'+data.style+'"></div>');
		}
		layMin.index = layMin.index + 1;
		var layIndex = layMin.index;
		layMin.indexList.push(layIndex);
		if(!data.icon && data.msg){
			if(data.msg.indexOf("失败") != -1 || data.msg.indexOf("错误") != -1){
				data.icon = '&#xe62b;';
				data.color = '#fd8383';
			}else if(data.msg.indexOf("警告") != -1 || data.msg.indexOf("异常") != -1){
				data.icon = '&#xe60f;';
				data.color = '#ffc876';
			}
		}
		data.icon = data.icon || '&#xe605;';
		var s = '';
		s += '<div class="lay-min-tips-box '+(data.anim?'lay-min-tips-box-anim':'')+' lay-min-tips-box-'+offset+' lay-min-index-'+layIndex+'" style="z-index:'+layIndex+';">';
		s += '	<div style="'+(data.timeDesc ? "padding: 12px 60px 10px 10px;" : "padding: 12px 30px 10px 10px;")+'">';
		s += '		<i class="lay-min-icon lay-min-tips-icon" style="color:'+data.color+'">'+data.icon+'</i>';
		s += '		<span class="lay-min-tips-text">'+(data.msg || '')+'</span>';
		s += '	<div class="lay-min-tips-line lay-min-tips-line-'+layIndex+'" style="'+(data.line ? "display:block" : "display:none")+'"></div>';
		s += '	<i class="lay-min-icon lay-min-tips-close lay-min-tips-close-'+layIndex+'" title="关闭">&#xeb6a;</i>';
		s += '	<span class="lay-min-tips-time-desc lay-min-tips-time-desc-'+layIndex+'" style="'+(data.timeDesc ? "display:block" : "display:none")+'">'+(data.time || 3)+'</span>';
		s += '</div>';
		$(".lay-min-tips-"+offset).append(s);
		// 点击关闭
		$('.lay-min-tips-close-'+layIndex).on("click", function(){
			layMin.close(layIndex, 100);
			layMin.stopPropagation();
		})
		if(offset == 'right'){
			setTimeout(function(){
				$('.lay-min-index-'+layIndex).css("right", "0%");
			}, 50)
		}
		// 倒计时关闭
		if(data.time){
			var tipsInterval;
			if(data.line){
				var timeNum = 0;
				var tempValue = 100;
				var tempTimeDesc = data.time;
				data.time = data.time*1000;
				tipsInterval = setInterval(function(){
					timeNum = timeNum + 10;
					var value = 10 / data.time * 100;
					tempValue = tempValue - value;
					$('.lay-min-tips-line-'+layIndex).css("width", tempValue+"%");
					if(timeNum%1000 == 0){
						tempTimeDesc --;
						$('.lay-min-tips-time-desc-'+layIndex).text(tempTimeDesc);
					}
					if(timeNum >= data.time){
						clearInterval(tipsInterval);
						layMin.close(layIndex);
					}
				}, 10)
				// 鼠标放上，移开事件（暂停倒计时使用）
				if(data.stype){
					$('.lay-min-index-'+layIndex).hover(function() {
						clearInterval(tipsInterval);
					}, function() {
						if(data.stype == 1){
							tipsInterval = setInterval(function(){
								timeNum = timeNum + 10;
								var value = 10 / data.time * 100;
								tempValue = tempValue - value;
								$('.lay-min-tips-line-'+layIndex).css("width", tempValue+"%");
								if(timeNum%1000 == 0){
									tempTimeDesc --;
									$('.lay-min-tips-time-desc-'+layIndex).text(tempTimeDesc);
								}
								if(timeNum >= data.time){
									clearInterval(tipsInterval);
									layMin.close(layIndex);
								}
							}, 10)
						}
					})
				}
			}else{
				var timeNum = 0;
				var tempTimeDesc = data.time;
				tipsInterval = setInterval(function(){
					timeNum ++;
					tempTimeDesc --;
					$('.lay-min-tips-time-desc-'+layIndex).text(tempTimeDesc);
					if(data.time == timeNum){
						clearInterval(tipsInterval);
						layMin.close(layIndex);
					}
				}, 1000)
				// 鼠标放上，移开事件（暂停倒计时使用）
				if(data.stype){
					$('.lay-min-index-'+layIndex).hover(function() {
						clearInterval(tipsInterval);
					}, function() {
						if(data.stype == 1){
							tipsInterval = setInterval(function(){
								timeNum ++;
								tempTimeDesc --;
								$('.lay-min-tips-time-desc-'+layIndex).text(tempTimeDesc);
								if(data.time == timeNum){
									clearInterval(tipsInterval);
									layMin.close(layIndex);
								}
							}, 1000)
						}
					})
				}
			}
		}
		// 鼠标放上显示关闭按钮，移开隐藏
		$('.lay-min-index-'+layIndex).mouseover(function() {
			$('.lay-min-tips-close-'+layIndex).css('display', 'block');
		}).mouseout(function() {
			$('.lay-min-tips-close-'+layIndex).css('display', 'none');
		});
		// 点击事件
		if(data.click){
			$('.lay-min-index-'+layIndex).css('cursor', 'pointer');
			$('.lay-min-index-'+layIndex).on('click', function(){
				data.click(this);
				layMin.stopPropagation();
			})
		}
		// 成功回调
		if(data.success){
			data.success();
		}
		return layIndex;
	},
	loadSubmit: function(data){
		$(data.elem).addClass("lay-min-btn-disabled");
        $(data.elem).html('<i class="lay-min-icon lay-min-anim lay-min-anim-rotate lay-min-anim-loop">&#xe8fd;</i>'+data.msg);
	},
	closeSubmit: function(data){
		$(data.elem).html(data.msg);
        $(data.elem).removeClass("lay-min-btn-disabled");
	},
	load: function(data){
		if(!data){
			data = {};
		}
		data.bg = data.bg || "";
		data.fontSize = data.fontSize || 35;
		data.color = data.color || "#76a6ff";
		data.layIcon = data.icon == 1 ? '&#xe8fc;' : '&#xe8fd;';
		layMin.index = layMin.index + 1;
		var layIndex = layMin.index;
		layMin.indexList.push(layIndex);
		var s = '';
		if(!data.type || data.type == 1){
			if(!data.shade){
				s += '<div class="lay-min-tow-load lay-min-index-'+layIndex+'" style="z-index:'+layIndex+';width:'+data.fontSize+'px;height:'+data.fontSize+'px;">';
				s += '	<i class="lay-min-icon lay-min-anim lay-min-anim-rotate lay-min-anim-loop" style="color:'+data.color+';font-size: '+data.fontSize+'px;">'+data.layIcon+'</i>';
				s += '</div>';
			}else{
				s += '<div class="lay-min-box lay-min-index-'+layIndex+'" style="z-index:'+layIndex+';background: '+data.bg+';opacity:'+data.shade+'">';
				s += '	<div class="lay-min-tow-load" style="width:'+data.fontSize+'px;height:'+data.fontSize+'px;">';
				s += '		<i class="lay-min-icon lay-min-anim lay-min-anim-rotate lay-min-anim-loop" style="color:'+data.color+';font-size: '+data.fontSize+'px;">'+data.layIcon+'</i>';
				s += '	</div>';
				s += '</div>';
			}
		}else if(data.type == 2){
			s += '<div class="lay-min-m-layer2 lay-min-index-'+layIndex+'" index="0" style="z-index:'+layIndex+';background: '+data.bg+';">';
			s += '	<div class="lay-min-m-layershade" style="background: '+data.bg+';"></div>';
			s += '	<div class="lay-min-m-layermain">';
			s += '		<div class="lay-min-m-layersection">';
			s += '			<div class="lay-min-m-layerchild  lay-min-m-anim-scale">';
			s += '				<div class="lay-min-m-layercont">';
			s += '					<i style="background-color:'+data.color+';width:'+data.fontSize+'px;height:'+data.fontSize+'px;"></i>';
			s += '					<i style="background-color:'+data.color+';width:'+data.fontSize+'px;height:'+data.fontSize+'px;" class="lay-min-m-layerload"></i>';
			s += '					<i style="background-color:'+data.color+';width:'+data.fontSize+'px;height:'+data.fontSize+'px;"></i>';
			s += '				</div>';
			s += '			</div>';
			s += '		</div>';
			s += '	</div>';
			s += '</div>';
		}else if(data.type == 3){
			s += '<div class="lay-min-loader lay-min-index-'+layIndex+'" style="z-index:'+layIndex+';background: '+data.bg+';">';
			s += '	<div class="lay-min-loader-inner"></div>';
			s += '</div>';
		}else if(data.type == 4){
			s += '<div class="lay-min-box lay-min-index-'+layIndex+'" style="z-index:'+layIndex+';background: '+data.bg+';">';
			s += '	<div class="lay-min-box-loading">';
			s += '        <span></span>';
			s += '        <span></span>';
			s += '        <span></span>';
			s += '        <span></span>';
			s += '    </div>';
			s += '</div>';
		}
		$(data.elem || "body").append(s);
		// 是否设置多少毫秒关闭
		if(data.time){
			setTimeout(function(){
				layMin.close(layIndex);
			}, data.time)
		}
		// 成功回调
		if(data.success){
			data.success();
		}
		return layIndex;
	},
	close: function(index, time){
		for(var i=0; i<layMin.indexList.length; i++){
			if(layMin.indexList[i] == index){
				$(".lay-min-index-"+index).fadeOut(time || 500);
				setTimeout(function(){
					$(".lay-min-index-"+index).remove();
				}, 500)
				layMin.indexList.splice(i, 1);
				break;
			}
		}
	},
	closeAll: function(time){
		for(var i=0; i<layMin.indexList.length; i++){
			var index = layMin.indexList[i];
			$(".lay-min-index-"+index).fadeOut(time || 500);
			setTimeout(function(){
				$(".lay-min-index-"+index).remove();
			}, 500)
		}
		for(var i=0; i<layMin.indexList.length; i++){
			layMin.indexList.splice(i, 1);
		}
	},
	stopPropagation: function(e) {// 阻止父级被子级点击触发
        e = e || window.event;  
        if(e.stopPropagation) { //W3C阻止冒泡方法  
            e.stopPropagation();  
        } else {  
            e.cancelBubble = true; //IE阻止冒泡方法  
        }  
    }
}

/**
 * -------------------------------------------------------------------------------------------------------------------------------
 * 是否是以layui组件方式引入
 */
if(layui){
	layui.define(['jquery'], function (exports) {
		var $ = layui.jquery;
		
		layui.link(layui.cache.base + '/layMin/layMin.css');
		
	    exports('layMin', layMin);
	});
}



